<?use pft\Prod\Product;
include_once('common/autoload.php');
include_once('/var/www/html/new/d/class/abc/Syslog.class.php');
if(!$_SESSION['memberID']){exit("<script>window.location='dlogin.html'</script>");}
$tpl=Tpl::getLink();
$did=$_REQUEST["did"];
$pri=$_REQUEST["pri"];
$pid=$_REQUEST["pid"]?$_REQUEST["pid"]:array();
$bind=$_REQUEST["bind"];
$tplid=$_REQUEST["tplid"];
$_pri=array();
if($bind && $tplid){
	if(($res=$tpl->apply($did,$tplid))===true){
		echo "<script>alert('配置成功')</script>";
	}
	else{
		echo "<script>alert('".$res."')</script>";
	}
}
else{
	foreach($pid as $v){
		if ($pri[$v] >= 10000) {
			exit("<script>alert('价格配置必须限制在10000以内!')</script>");
		}
		$_pri[$v]=$pri[$v];
	}
	abc777\Syslog::write_log(json_encode($_REQUEST),'priceset');
	$pids=array_unique(array_keys($_pri));
	$sel="select pids from pft_product_sale_list where fid=".$did." and aid=".$_SESSION['sid']." limit 1";
	$GLOBALS['le']->query($sel);
	$GLOBALS['le']->fetch_assoc();
	$diff=array_diff(explode(",",$GLOBALS['le']->f("pids")),$pids, array(null,'null','',' '));
	$upd="update pft_product_sale_list set pids='".implode(",",$pids)."' where fid=".$did." and aid=".$_SESSION['sid'];
	$GLOBALS['le']->query($upd);
	$logStr = $_SESSION['sid'].'|'.$_SESSION['memberID'].'|'.$upd;
	abc777\Syslog::write_log($logStr,'pft_product_sale_list');
	if(!$_pri){exit("<script>alert('配置成功!')</script>");}
	$upd="update uu_fx_tpl_cs set tplid=0 where fxid=".$did." limit 1";
	$GLOBALS['le']->query($upd);    
	if(($res=$tpl->unusual($did,$_pri))===true){
		if($diff){
			$sel="select pid,pft_rflex_id from uu_jq_ticket where pid in (".implode(",",$diff).")";
			$GLOBALS['le']->query($sel);             
			while($row=$GLOBALS['le']->fetch_assoc()){
				$del[]=$row['pid'];
			}
			if($del){
				$pro = new Product($GLOBALS['le']);
                 
//				$ret = json_decode($pro->SyncTicketState(2, implode(",",$del)),true);
//				if($ret['status']!="ok"){
//					exit("<script>alert('".$ret['msg']."')</script>");
//				}
			}
		}              
		echo "<script>alert('配置成功!');parent.window.location.href='".$_SERVER['HTTP_REFERER']."'</script>";
	}
	else{
		echo "<script>alert('".$res."')</script>";
	}
}
?>